package com.example.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * 死信队列产生条件
 * 消息过了过期时间TTL（time to live）
 * 消息队列达到了最大长度
 * 消息被消费者拒绝（basic.reject或者basic.nack）且requeue=false
 */
@Slf4j
@Component
public class TtlQueueConsumer {

    //消息过了过期时间TTL（time to live）
    //只需要设置消息的过期时间，普通队列在过期时间之内没有收到消息，则消息会成为死信并被转发到死信队列中。关于过期时间我所了解到的有两种设置方式：
    @RabbitListener(queues = "QD")
    public void receiveD(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        log.info("当前时间：{},收到死信队列信息{}", new Date().toString(), msg);
    }

    /*@RabbitListener(queues = "QA")
    public void t1(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        log.info("当前时间：{},QA正常接收队列不到死信{}", new Date().toString(), msg);
    }
    @RabbitListener(queues = "QB")
    public void t2(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        log.info("当前时间：{},QB正常接收队列不到死信{}", new Date().toString(), msg);
    }*/

   /* @RabbitListener(queues = "QC")
    public void t3(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        log.info("当前时间：{},QC正常接收队列不到死信{}", new Date().toString(), msg);
    }*/
}
